package com.zhijiang.hospital.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.zhijiang.common.util.BaseQuery;
import com.zhijiang.common.util.file.FileUtil;
import com.zhijiang.common.util.PageList;
import com.zhijiang.hospital.entrty.*;
import com.zhijiang.hospital.entrty.dto.EmployeeDto;
import com.zhijiang.hospital.mapper.EmpolyeeMapper;
import com.zhijiang.hospital.mapper.MeetingEmployeeMapper;
import com.zhijiang.hospital.mapper.MeetingMapper;
import com.zhijiang.hospital.service.MeetingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project zhiJiangHealthBureau
 * @description
 * @date 2023-01-10 09:20:07
 */
@Service
public class MeetingServiceImpl implements MeetingService {
    @Resource
    private MeetingMapper meetingMapper;
    @Resource
    EmpolyeeMapper empolyeeMapper;
    @Resource
    MeetingEmployeeMapper meetingEmployeeMapper;



    @Override
    public PageList<Meeting> findAll(BaseQuery baseQuery) {
        PageList<Meeting> pageList = new PageList<>();
        List<Meeting> list = meetingMapper.findAll();
        if (list.isEmpty())
            return null;
        List<Meeting> collect = list.stream().sorted(Comparator.comparing(Meeting::getMBeginDate).reversed()).collect(Collectors.toList());
        List<Meeting> meetingList = collect.stream().
                skip((baseQuery.getPage() - 1) * baseQuery.getPageSize()).
                limit(baseQuery.getPageSize()).collect(Collectors.toList());
        pageList.setTotal(meetingList.size());
        pageList.setList(meetingList);
        return pageList;
    }

    @Override
    public Integer insertMeeting(Meeting meeting) {
        ComEmployee comEmployee = empolyeeMapper.selectOne(meeting.getMOperCode());
        meeting.setMOperName(comEmployee.getEmplName());
        meeting.setMKey(RandomUtil.randomNumbers(4));
        meeting.setMFlushed(2);

        return meetingMapper.insertMeeting(meeting);
    }

    @Override
    public Meeting selectMeeting(String mCode) {
        return  meetingMapper.selectMeeting(mCode);
    }

    @Override
    public List<Meeting> selectMeetingList(String startTime, String endTime) {
        List<Meeting> meetingList = meetingMapper.selectMeetingList(startTime, endTime);
        List<Meeting> collect = meetingList.stream().sorted(Comparator.comparing(Meeting::getMBeginDate).reversed()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<String> findMCode(String mCode) {
        return meetingMapper.findMCode(mCode);
    }

    @Override
    public void meetingApproval(String mCode, String emplCode) {
        ComEmployee comEmployee = empolyeeMapper.selectOne(emplCode);
        SMeetingcheckrecode one = meetingEmployeeMapper.getOne(mCode, emplCode, comEmployee.getEmplName());
        if (one.getPValidity().equals("5")){
            meetingMapper.meetingApproval(mCode, emplCode);
        }else {
            return;
        }

    }

    @Override
    public List<EmployeeDto> leaveEmployeeNumber(String mCode) {
        List<EmployeeDto> employeeDtos = meetingMapper.findMeeting(mCode);
        return employeeDtos;
    }
    //@Transactional
    //@Override
    //public boolean batchMeetingApproval(List<SMeetingcheckrecode> list, String mCode) {
    //
    //    Integer num=0;
    //    for (SMeetingcheckrecode sMeetingcheckrecode : list) {
    //        meetingMapper.meetingApproval(mCode, sMeetingcheckrecode.getPCode());
    //        num++;
    //    }
    //    if (list.size()==num){
    //        return true;
    //    }else {
    //        return false;
    //    }
    //}
    private static int max = 140 ;
    @Override
    public void update(String mCode, String text) {

                meetingMapper.updateText(mCode,text);

    }

    @Override
    public void update1(String mCode, String text) {
        meetingMapper.updateText2(mCode,text);
    }

    @Override
    public void addFile(String mCode,MultipartFile file) throws IOException {
        System.out.println(file.getName()+"===="+file.getOriginalFilename()+"==="+file.getContentType()+"===="+file.getSize());

        //原始文件名
        String originalFilename = file.getOriginalFilename();//abc.jpg
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        byte[] bytes = file.getBytes();

        Meeting meeting = meetingMapper.selectMeeting(mCode);
        if (meeting.getMFile()!=null){
            throw new RuntimeException("文件已存在");
        }
        File file1 = FileUtil.multipartFile2File(file);
        byte[] bytes1 = FileUtil.file2byte(file1);
        String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        Integer integer = meetingMapper.update(mCode,bytes1, file.getOriginalFilename());
    }


    @Override
    public void batchMeetingApproval(String mCode, List<SMeetingcheckrecode> dto) {
        for (SMeetingcheckrecode sMeetingcheckrecode : dto) {
            SMeetingcheckrecode one = meetingEmployeeMapper.getOne(mCode, sMeetingcheckrecode.getPCode(), sMeetingcheckrecode.getPName());
            if (one !=null &&one.getPValidity().equals("5")){
                meetingMapper.meetingApproval(mCode, sMeetingcheckrecode.getPCode());
            }
        }
    }

    @Override
    public List<EmployeeDto> findLeave(String mCode) {
        return meetingMapper.findLeave(mCode);
    }

    @Override
    public void setFilePh(String
                                      mCode,String s) {

       meetingMapper.setPh(mCode,s);
    }

    @Override
    public void setMeetingFish(String mCode, int time) {
        meetingMapper.setTime(mCode,time);

    }

    @Override
    public void setTwoSign(Meeting meeting) {
        meetingMapper.setTwoSign(meeting);
    }

    @Override
    public void updateTime(String mCode, String s) {
        meetingMapper.updatetime(mCode,s);
    }

    public static void main(String[] args) {

    }
    @Override
    public void importExcel(MultipartFile file,String mCode) throws IOException {
        System.out.println(file.getName()+file.getSize()+file.getInputStream());
        Meeting meeting = meetingMapper.selectMeeting(mCode);
        List<OutsideEmployee> objects = EasyExcel.read(file.getInputStream()).head(OutsideEmployee.class).sheet().headRowNumber(1).doReadSync();
        for (OutsideEmployee object : objects) {
            SMeetingcheckrecode sMeetingcheckrecode = new SMeetingcheckrecode();
            sMeetingcheckrecode.setPName(object.getName());
            sMeetingcheckrecode.setMCode(object.getPPhone().substring(5));
            sMeetingcheckrecode.setPFirm(object.getPFirm());
            sMeetingcheckrecode.setPPhone(object.getPPhone());
            sMeetingcheckrecode.setMCode(mCode);
            sMeetingcheckrecode.setMBegindate(meeting.getMBeginDate());
            sMeetingcheckrecode.setMAddress(meeting.getMAddress());
            sMeetingcheckrecode.setPValidity("0");
            sMeetingcheckrecode.setPRemark3("非职工");
            sMeetingcheckrecode.setMName(meeting.getMName());
            meetingEmployeeMapper.addOutSideEmployee(sMeetingcheckrecode);
        }
    }


}
